package me.jessyan.armscomponent.app.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.app.R;
import me.jessyan.armscomponent.app.di.component.DaggerCommonWebViewComponent;
import me.jessyan.armscomponent.app.mvp.contract.learn.CommonWebViewContract;
import me.jessyan.armscomponent.app.mvp.presenter.learn.CommonWebViewPresenter;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * <p>
 * Author: Taozebi
 * Date: 08/08/2021 15:36
 * Description: 公共网页面显示页面
 */

@Route(path = RouterHub.APP_COMMONWEBVIEWACTIVITY)
public class CommonWebViewActivity extends BaseActivity<CommonWebViewPresenter> implements CommonWebViewContract.View {


    @BindView(R.id.common_webview)
    WebView common_webview;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerCommonWebViewComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_common_webview; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        WebSettings webSettings = common_webview.getSettings();
        webSettings.setDomStorageEnabled(true);

        Intent intent = getIntent();
        if (intent != null && intent.hasExtra("url"))
        {
            String title = intent.getStringExtra("title");
            setTitle(title);
            String url = intent.getStringExtra("url");
            common_webview.loadUrl(url);
            // 设置WebViewClient来接收处理请求和通知
            common_webview.setWebViewClient(new WebViewClient(){
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    // 返回true则表明使用的是WebView
                    return true;
                }
            });

            common_webview.requestFocus();
        }

    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

}
